//
//  PresentAnimator.m
//  KanManHua
//
//  Created by Banning on 2018/7/8.
//  Copyright © 2018年 KanManHua. All rights reserved.
//

#import "PresentAnimator.h"

@implementation PresentAnimator

+(PresentAnimator *)animatorFromRight:(BOOL)fromRight{
    PresentAnimator *animator = [PresentAnimator new];
    animator.edgeType = UIRectEdgeNone;
    animator.fromRight = fromRight;
    return animator;
}


- (instancetype)init {
    
    self = [super init];
    if (self) {
        
        // 默认参数设置
        self.edgeType = UIRectEdgeLeft;
    }
    
    return self;
}

- (NSTimeInterval)transitionDuration{
    return 0.35f;
}

- (void)present{
    [super present];
    [self.containerView addSubview:self.toViewController.view];
    [self.containerView setBackgroundColor:[UIColor whiteColor]];
    
    CGRect originalFrame = self.toViewController.view.frame;
    CGRect newFrame = CGRectZero;
    if (self.fromRight) {
        newFrame = CGRectMake([UIScreen mainScreen].bounds.size.height, 0, originalFrame.size.width, originalFrame.size.height);
    }else{
        newFrame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, originalFrame.size.width, originalFrame.size.height);
    }
    
    self.toViewController.view.frame = newFrame;
    
    [UIView animateWithDuration:self.transitionDuration animations:^{
        self.toViewController.view.frame = originalFrame;
    } completion:^(BOOL finished) {
        [self completeTransition];
    }];
}

- (void)dismiss{
    [super dismiss];
    [self.containerView addSubview:self.toViewController.view];
    [self.containerView addSubview:self.fromViewController.view];
    CGRect originalFrame = self.fromViewController.view.frame;
    CGRect dismissFrame = CGRectZero;
    if (self.fromRight) {
        dismissFrame = CGRectMake([[UIScreen mainScreen] bounds].size.width, 0, originalFrame.size.width, originalFrame.size.height);
    }else{
        dismissFrame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, originalFrame.size.width, originalFrame.size.height);
    }
    
    [UIView animateWithDuration:self.transitionDuration animations:^{
        self.fromViewController.view.frame = dismissFrame;
    } completion:^(BOOL finished) {
        [self.containerView addSubview:self.toViewController.view];
        [self completeTransition];
    }];
}

- (void)setFromRight:(BOOL)fromRight
{
    _fromRight = fromRight;
//    self.edgeType = fromRight ? UIRectEdgeLeft : UIRectEdgeTop;
}

@end
